package process

import (
	"chat/common/msg"
	"chat/server/model"
	"chat/utils"
	"encoding/json"
	"fmt"
	"net"
)

type UserProcess struct {
	Conn   net.Conn
	UserId int
}

func (this *UserProcess) ServerProcessLogin(message *msg.Message) (err error) {
	var loginMsg msg.LoginMsg
	err = json.Unmarshal([]byte(message.Data), &loginMsg)
	if err != nil {
		fmt.Println("serverProcessLogin Unmarshal error = ", err)
		return
	}
	// 返回信息
	var responseMsg msg.Message
	responseMsg.Type = msg.LoginMsgType
	// 消息体
	var loginResMsg msg.LoginResMsg

	// 校验用户名密码
	userDao := model.MyUserDao
	user, err := userDao.Login(loginMsg.UserId, loginMsg.Pwd)
	if err != nil {
		loginResMsg.Code = 500
		loginResMsg.Error = "用户不存在"
	} else {
		loginResMsg.Code = 200
		fmt.Println("ServerProcessLogin success user = ", user)
		// 登陆成功后，把用户放到manager管理
		this.UserId = loginMsg.UserId
		userManager.AssOnlineUser(this)
		// 存放在线用户
		for id, _ := range userManager.onlineUsers {
			loginResMsg.UserIds = append(loginResMsg.UserIds, id)
		}
		fmt.Println("登陆成功，通知其他在线用户")
		// 通知其他 在线用户
		this.NotifyOtherOnlineUser(loginMsg.UserId)
	}
	// 序列化消息体
	data, err := json.Marshal(loginResMsg)
	if err != nil {
		fmt.Println("serverProcessLogin loginResMsg Marshal error = ", err)
		return
	}
	responseMsg.Data = string(data)
	// 整个消息
	data, err = json.Marshal(responseMsg)
	if err != nil {
		fmt.Println("serverProcessLogin responseMsg Marshal error = ", err)
		return
	}
	// 发送消息
	tf := &utils.Transfer{
		Conn: this.Conn,
	}
	err = tf.WritePkg(data)
	if err != nil {
		fmt.Println("serverProcessLogin writePkg Marshal error = ", err)
	}
	return
}

func (this *UserProcess) ServerProcessRegister(message *msg.Message) (err error) {
	var registerMsg msg.RegisterMsg
	err = json.Unmarshal([]byte(message.Data), &registerMsg)
	if err != nil {
		fmt.Println("ServerProcessRegister Unmarshal error = ", err)
		return
	}
	// 返回信息
	var responseMsg msg.Message
	responseMsg.Type = msg.RegisterResMsgType
	// 消息体
	var registerResMsg msg.RegisterResMsg

	// 注册-入库
	userDao := model.MyUserDao
	err = userDao.Register(&registerMsg.User)
	if err != nil {
		if err == model.ERROR_USER_EXISTS {
			registerResMsg.Code = 400
			registerResMsg.Error = model.ERROR_USER_EXISTS.Error()
		} else {
			registerResMsg.Code = 400
			registerResMsg.Error = "系统异常"
		}
	} else {
		registerResMsg.Code = 200
	}
	// 序列化消息体
	data, err := json.Marshal(registerResMsg)
	if err != nil {
		fmt.Println("ServerProcessRegister registerResMsg Marshal error = ", err)
		return
	}
	responseMsg.Data = string(data)
	// 整个消息
	data, err = json.Marshal(responseMsg)
	if err != nil {
		fmt.Println("ServerProcessRegister responseMsg Marshal error = ", err)
		return
	}
	// 发送消息
	tf := &utils.Transfer{
		Conn: this.Conn,
	}
	err = tf.WritePkg(data)
	if err != nil {
		fmt.Println("ServerProcessRegister writePkg Marshal error = ", err)
	}
	return
}

// 用户状态通知  通知其他在线用户
func (this *UserProcess) NotifyOtherOnlineUser(userId int) {
	// 遍历 userManager.onlineUsers 然后通知
	for id, up := range userManager.onlineUsers {
		if id == userId {
			continue
		}
		// 开始通知  up 为各个用户的UserProcess
		up.NotifyToOther(userId)
	}
}

func (this *UserProcess) NotifyToOther(userId int) {
	var mes msg.Message
	mes.Type = msg.NotifyUserStatusType
	var notifyUserStatus msg.NotifyUserStatus
	notifyUserStatus.UserId = userId
	notifyUserStatus.Status = msg.UserOnline
	data, err := json.Marshal(notifyUserStatus)
	if err != nil {
		fmt.Println("NotifyToOther Marshal notifyUserStatus error = ", err)
		return
	}
	mes.Data = string(data)

	data, err = json.Marshal(mes)
	if err != nil {
		fmt.Println("NotifyToOther Marshal mes error = ", err)
		return
	}
	tf := &utils.Transfer{
		Conn: this.Conn,
	}
	err = tf.WritePkg(data)
	if err != nil {
		fmt.Println("NotifyToOther WritePkg mes error = ", err)
	}
}
